package com.zhongxiangspace.tools;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取用户真是ip
 *
 * @author Administrator
 */
public class IPAddrUtil {
    // 获得客户端真实IP地址的方法二：
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        String str = "127.0.0.1";
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || str.indexOf(ip) > -1) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || str.indexOf(ip) > -1) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || str.indexOf(ip) > -1) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || str.indexOf(ip) > -1) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || str.indexOf(ip) > -1) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
